// pages/car/mine/editData/editData.js
const app = getApp()
Page({

    data: {
        // imgList:'/images/idImg.png',
        imgListTwo: '/images/driverImg.png',
        imgListThree: '/images/photo.png',
        imgListFour: '/images/drivinglicense.png',
        is_sendcode: false,// 60s
        time_count: 60,
        name: '', // 姓名
        phone: '', // 手机号
        plate: '', // 车牌
        code:'',//验证码
        examine: ''// 是否认证
    },
    onLoad: function (options) {
      this.ownerdata();
      this.getStatus()
    },
    //  姓名
    inputName(event){
        // console.log(event);
        this.setData({
            name: event.detail.value
        })
        // console.log(this.data.name)
    },
     // 手机号
     inputNumber(event){
      // console.log(event);
      this.setData({
          phone: event.detail.value
      })
      // console.log(this.data.phone)
  },
  // 车牌号
  inputPlateNumber(event){
      // console.log(event);
      this.setData({
          plate: event.detail.value
      })
      // console.log(this.data.plate)
  },
    // 验证码
    inputCode(event){
      // console.log(event);
      this.setData({
        code: event.detail.value
      })
    },
    // 验证码
    code(){
      console.log(11111)
      if(!/^1[3456789]\d{9}$/.test(this.data.phone)){
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
          mask: true
        })
        return false;
      }
      wx.request({
        url: app.globalData.url + 'api/v1/owner/login/owner-code',
        method: 'GET',
        data:{
          phone: this.data.phone
        },
        success: res => {
          // console.log(res)
          if(res.data.code == 200){
            this.getSendCode();
            wx.showToast({
              title: '发送成功',
              icon: 'none',
              mask: true
            })
          } else {
            wx.showToast({
              title: res.data.message,
              icon: 'none',
              mask: true
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      })   
    },
    // 60s
    getSendCode() {
      const TIME_COUNT = 60;
      if (!this.timer) {
        this.setData({
          time_count: TIME_COUNT,
          is_sendcode: true
        })
        this.data.timer = setInterval(() => {
          if (this.data.time_count > 0 && this.data.time_count <= TIME_COUNT) {
              this.setData({
                time_count: this.data.time_count-1
              })
          } else {
              this.setData({
                is_sendcode: false,
                timer: null
              })
              clearInterval(this.data.timer);
          }
        }, 1000);
      }
    },
    // 上传图片
    uploadImg(e){
        let cramer = e.currentTarget.dataset.type;   
        wx.chooseImage({
          sizeType: "compressed", //压缩图
          count: 1,
          success: (res) => {
            console.log(res)
            let path = res.tempFilePaths;
            if(cramer == 1){
              this.setData({
                imgListTwo: path[0]
              })
            }else if(cramer == 2){
              this.setData({
                imgListThree: path[0]
              })
            } else if(cramer == 3){
              this.setData({
                imgListFour: path[0]
              })
            }
            wx.uploadFile({
              url: app.globalData.url + 'api/v1/file/files',
              method: 'POST',
              filePath: path[0],
              name: 'file',
              success: res => {
                // console.log(res)
                let arr = JSON.parse(res.data);
                // console.log(arr)
                if(cramer == 1){
                  this.setData({
                    imgListTwo: arr.data.url
                  })
                }else if(cramer == 2){
                  this.setData({
                    imgListThree: arr.data.url
                  })
                }
                else if(cramer == 3){
                  this.setData({
                    imgListFour: arr.data.url
                  })
                }
              },
              fail: err => {
                console.log(err)
              }
            })
          }
        })
    },
    // 已认证，不能修改图片
    uploadImgs(){
      if(this.data.examine == 2) {
        wx.showToast({
          title: '修改资料请联系客服',
          icon: 'none',
          mask: true
        })
      }
    },
    // 获取信息
    ownerdata(){
      wx.showLoading({
        title: '加载中···',
      })
      wx.request({
        url: app.globalData.url + 'api/v1/owner/login/ownerdata',
        method: 'GET',
        data:{
          id: wx.getStorageSync('id')
        },
        success: res => {
          // console.log(res,12);
          this.setData({
            // imgList: res.data.data.positive,
            imgListTwo: res.data.data.driver,
            imgListThree: res.data.data.picture,
            imgListFour: res.data.data.driving,
            name: res.data.data.name,
            phone: res.data.data.phone,
            plate: res.data.data.plate
          })
        },
        fail: err => {
          console.log(err)
        },
        complete: () => {
          wx.hideLoading()
        }
      })   
    }, 
    // 提交 修改资料
    submit(){
      console.log(this.data.code)
      if ( this.data.imgTwo == "" ){
        wx.showToast({
          title: '请上传驾驶证正面',
          icon: 'none',
        })
        return false
      } else if ( this.data.imgThree == "" ){
        wx.showToast({
          title: '请上传本人照片',
          icon: 'none',
        })
        return false
      } else if ( this.data.imgFour == "" ){
        wx.showToast({
          title: '请上传行驶证',
          icon: 'none',
        })
        return false
      } else if ( this.data.name == "" ){
        wx.showToast({
          title: '请输入您的姓名',
          icon: 'none',
        })
        return false
      } else if ( !/^[\u4e00-\u9fa5]{0,16}$/.test(this.data.name) ){
        wx.showToast({
          title: '请正确输入您的姓名',
          icon: 'none',
        })
        return false
      } else if ( this.data.phone == "" ){
        wx.showToast({
          title: '请输入您的号码',
          icon: 'none',
        })
        return false
      } else if (!/^1[3456789]\d{9}$/.test(this.data.phone) ){
        wx.showToast({
          title: '手机号格式不正确',
          icon: 'none',
        })
        return false
      } else if ( this.data.code == "" ){
        wx.showToast({
          title: '请发送验证码',
          icon: 'none'
        })
        return false
      } else if ( this.data.plate == ''){
        wx.showToast({
          title: '请输入您的车牌号',
          icon: 'none',
        })
        return false
      } else if(!/^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/.test( this.data.plate)){
        wx.showToast({
          title: "请输入正确的车牌号",
          icon: "none",
        })
        return false
      }
      wx.request({
        url: app.globalData.url + 'api/v1/owner/login/owneredit',
        method: 'POST',
        data:{
            id: wx.getStorageSync('id'),
            open_id: wx.getStorageSync('openid'),
            // positive: this.data.imgList,
            driver: this.data.imgListTwo,
            picture: this.data.imgListThree,
            driving: this.data.imgListFour,
            name: this.data.name,
            phone: this.data.phone,
            code: this.data.code,
            plate: this.data.plate  
        },
        success: res => {
          // console.log(res,999)
          if (res.data.code == 200){
              wx.showToast({
                  title: '修改成功',
                  icon: 'none',
              })
              setTimeout(() => {
                  wx.navigateBack({
                    delta: 1,
                  })
              },500)
          } else{
              wx.showToast({
                  title: res.data.message,
                  icon: 'none',
              })
          }
        },
        fail: err => {
          console.log(err)
        }
      })   
    },
    // 获取是否认证
    getStatus(){
      wx.request({
          url: app.globalData.url + 'api/v1/owner/owner/informationc',
          method: 'GET',
          data:{
            id: wx.getStorageSync('id')
          },
          success: res => {
              // console.log(res.data.data.examine,999)
              this.setData({
                  examine: res.data.data.examine
              })
          },
          fail: err => {
              console.log(err)
          },
      })   
    },

     /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    } 
})